package com.djhu.es.core.request;
/**
 * Created by zw on 2017/12/28.
 */

import com.djhu.es.core.annotation.LogicType;
import com.djhu.es.core.annotation.QueryAnnotation;
import com.djhu.es.core.annotation.QueryType;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.QueryBuilder;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * <p>
 * ---------------------------------
 */
public class AutoGeneratorSearchRequest extends AbstractRequest implements SearchRequest{

    private Class aClass;
    private Object obj;

    public AutoGeneratorSearchRequest(){}
    public AutoGeneratorSearchRequest(Object obj) {
        this.obj = obj;
    }

    public AutoGeneratorSearchRequest(Class aClass, Object obj) {
        this.aClass = aClass;
        this.obj = obj;
    }

    @Override
    public QueryBuilder queryBuilder() {
        if (obj == null){
            throw new RuntimeException("generator error");
        }
        Field[] fields =  obj.getClass().getDeclaredFields();
        List<SearchRequest> searchRequests = new ArrayList<>();
        for (Field field:fields){
            QueryAnnotation annotation = field.getAnnotation(QueryAnnotation.class);

            if (annotation != null){
                String filedName = null;
                Object value = null;
                if (annotation.QueryType() == QueryType.TERMQUERY){
                    field.setAccessible(true);
                    filedName = field.getName();
                    if (!StringUtils.isEmpty(annotation.alias())){
                        filedName = annotation.alias();
                    }
                    filedName = filedName+".keyword";
                    try {
                        value =  field.get(obj);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                        continue;
                    }
                }

                if (value == null || StringUtils.isEmpty(filedName)){
                    continue;
                }
                SearchRequest usedsearchRequest = null;
                SearchRequest logicsearchRequest = null;
                if (annotation.QueryType() == QueryType.TERMQUERY){
                    usedsearchRequest = new TermSearchRequest(getIndex(),getType(),getTimeout(),filedName,value);
                }else if (annotation.QueryType() ==QueryType.MATCHQUERY){
                    usedsearchRequest = new FulltextSearchRequest(getIndex(),getType(),getTimeout(), Arrays.asList(filedName),value);
                }

                if (annotation.LogicType() == LogicType.MUST){
                    logicsearchRequest = new MustSearchRequest(usedsearchRequest);
                }else if (annotation.LogicType() == LogicType.SHOULD){
                    logicsearchRequest = new ShouldSearchRequest(usedsearchRequest);
                }else if (annotation.LogicType() == LogicType.MUSTNOT){

                }
                searchRequests.add(logicsearchRequest);
            }

        }
        BoolSearchRequest boolSearchRequest = new BoolSearchRequest(searchRequests);
        return boolSearchRequest.queryBuilder();
    }



    public static void main(String[] args) {


    }
}